<template>
  <div class="app-container">
    <el-form ref="settlementForm" v-loading="loading" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="税源名称">
        {{ form.taxCompanyName }}
      </el-form-item>
      <el-form-item label="任务名称">
        {{ form.taskName }}
      </el-form-item>
      <el-form-item label="发票类型" prop="invoiceType">
        {{ form.invoiceType | invoiceTypeName }}
      </el-form-item>
      <el-form-item label="开票类目" prop="invoiceCatagory">
        {{ form.invoiceCatagory }}
      </el-form-item>

      <el-form-item label="结算费率" prop="invoiceCatagory">
        {{ form.merchantRate }}%
      </el-form-item>
      <el-form-item label="结算账户" prop="productId">
        <el-select v-model="form.productId" @change="handleAliTransferAccount">
          <el-option v-for="(item, index) in productList" :key="index" :label="getLabelText(item)" :value="item.productId" />
        </el-select>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button type="primary" :loading="loading" @click="subSettlement">立即结算</el-button>
      <el-button :loading="loading" @click="handleCancel(false)">取 消</el-button>
    </div>
  </div>
</template>
<script>
import { getRate } from "@/api/system/merchant";
import {  settlementTask } from "@/api/system/task"
import { listTransferAccountMerchantBalance } from "@/api/transfer/transferAccountMerchantBalance";


export default {
  name:"taskSettlement",
  props:{
    formData:{
      type:Object,
      default: {},
    }
  },
  data() {
    return {
      loading: false,
      form:{
        taskName:null,
        merchantRate:0,
        productId:null,
        taxCompanyName:null,
        transferAccountId:null,
      },
      productList:[],
      rules: {
        productId:[
          { required: true, message: "请选择结算账户", trigger: ["blur", "change"] }
        ],
      }
    }
  },
  filters: {
    invoiceTypeName(value) {
      switch (value) {
        case 1: return '专票';
        case 2: return '普票';
        default: return '';
      }
    }
  },
  methods:{
    getLabelText(item) {
      const balance = item.balance || 0;
      return `${item.productName}(${balance}元)`;
    },
    handleAliTransferAccount(val) {
      this.productList.forEach(item => {
        if (item.productId === val) {
          this.form.transferAccountId = item.transferAccountId
        }
      })
    },
    subSettlement(){
      this.$refs["settlementForm"].validate(valid => {
        if (valid) {
          this.loading = true
          settlementTask(this.form).then(res => {
            this.handleCancel(true)
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    handleCancel(val){
      this.$emit("cancel",val);
    }
  },
  watch:{
    formData:{
      handler(val){
        if (val.taskNo){
          this.loading = true
          this.form.taskNo = val.taskNo
          this.form.taskName = val.taskName
          this.form.invoiceType = val.invoiceType
          this.form.taxCompanyName = val.taxCompanyName
          this.form.invoiceCatagory = val.invoiceCatagory
          getRate(val.taxOrganizationId).then(res => {
            this.form.merchantRate = res.data
          })
          listTransferAccountMerchantBalance({organizationId:val.merchantOrganizationId,taxOrganizationId:val.taxOrganizationId}).then(res => {
            this.loading = false
            this.productList = res.rows
            if (res.rows.length > 0) {
              this.form.productId = res.rows[0].productId
              this.handleAliTransferAccount(this.form.productId)
            }
          })
        }
      },
      deep: true,
      immediate: true,
    }
  }
}

</script>
<style scoped lang="scss">
.dialog-footer{
  bottom: 10px;
  box-shadow: none;
  padding-bottom: 10px;
}
</style>

<style>
.el-dialog__body{
  padding-top: 0px;
}
</style>
